package sse.ustc.cc.db;

import java.util.ArrayList;

import sse.ustc.cc.fragment.WeekClassInfoFrag;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBService {

	private static final String TAG = "DBService";
	private DBHelper helper;
	private SQLiteDatabase db;
	private ArrayList<String> classList;
	private ArrayList<String> settingList;

	public DBService(Context context) {
		System.out.println("创建DBHelper");
		helper = new DBHelper(context);
	}

	/* 插入课程表，测试 */
	public void insertMsg() {
		db = helper.getWritableDatabase();

		//db.execSQL("insert into Teacher(Teacher_name,Teacher_room,Teacher_tel,Teacher_mail,Tutor_name,Tutor_tel,Tutor_mail) values('朱洪军','思贤楼501-6','18613727851','123@qq.com','宋超','18613727851','123@qq.com')");
		//db.execSQL("insert into Teacher(Teacher_name,Teacher_room,Teacher_tel,Teacher_mail,Tutor_name,Tutor_tel,Tutor_mail) values('李春杰','思贤楼501-6','18613727851','123@qq.com','宋超','18613727851','123@qq.com')");
		//db.execSQL("insert into Course(Course_name,Week,Section_begin,Week_begin,Teacher_name ,Course_alarm_time,Course_exam_time,Course_other,Room_name,Section_end,Week_end, Course_begin_time , Course_end_time ) values('嵌入式软件设计','2','6','2','朱洪军','15','第11周','幽默','明德楼','7','10','14:00','16:25')");
		//db.execSQL("insert into Course(Course_name,Week,Section_begin,Week_begin,Teacher_name,Course_alarm_time,Course_exam_time,Course_other,Room_name,Section_end,Week_end, Course_begin_time , Course_end_time ) values('嵌入式系统设计','1','6','2','李春杰','15','第11周','幽默','思贤楼','7','10','14:00','16:25')");
		db.execSQL("insert into Setting(Current_week  , Max_section , Section_time ) values('9','13','45')");
		db.close();
	}
	/* 插入老师，有条件筛选 */
	public void insertTeacher(String t_name, String t_room, String t_tel,
			String t_mail, String tu_name, String tu_tel, String tu_mail) {
		db = helper.getWritableDatabase();
		ContentValues cv = new ContentValues();

		cv.put("Teacher_name", t_name);
		cv.put("Teacher_room", t_room);
		cv.put("Teacher_tel", t_tel);
		cv.put("Teacher_mail", t_mail);
		cv.put("Tutor_name", tu_name);
		cv.put("Tutor_tel", tu_tel);
		cv.put("Tutor_mail", tu_mail);


		db.insert("Teacher", null, cv);
	}
	/* 插入课程表，有条件筛选 */
	public void insertCourse(String c_name, String week, String s_begin,
			String w_begin, String t_name, String c_alarm, String c_exam,
			String c_other, String r_name, String s_end,
			String w_end, String c_begin, String c_end) {
		db = helper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("Course_name", c_name);
		cv.put("Week", week);
		cv.put("Section_begin", s_begin);
		cv.put("Week_begin", w_begin);
		cv.put("Teacher_name", t_name);
		cv.put("Course_alarm_time", c_alarm);
		cv.put("Course_exam_time", c_exam);
		cv.put("Course_other", c_other);
		cv.put("Room_name", r_name);	
		cv.put("Section_end", s_end);
		cv.put("Week_end", w_end);
		cv.put("Course_begin_time", c_begin);
		cv.put("Course_end_time", c_end);
		db.insert("Course", null, cv);
	}

	/* 选择Setting，无条件筛选 */
	public ArrayList<String> selectSetting() {
		db = helper.getReadableDatabase();
		settingList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT* FROM Setting", new String[] {});
		System.out.println("Setting--->>" + cursor.getCount());
		while (cursor.moveToNext()) {
			String current_week = cursor.getString(cursor
					.getColumnIndex("Current_week"));
			String max_section = cursor.getString(cursor
					.getColumnIndex("Max_section"));
			String section_time = cursor.getString(cursor
					.getColumnIndex("Section_time"));
			//Log.e("current_time", current_week + "");
			settingList.add(current_week);
			settingList.add(max_section);
			settingList.add(section_time);
		}
		cursor.close();
		db.close();
		return settingList;

	}

	/* 选择课程表，无条件筛选 */
	public ArrayList<String> selectCourse() {
		db = helper.getReadableDatabase();
		classList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("SELECT * FROM Course", new String[] {});
		System.out.println("xxxx--->>" + cursor.getCount());
		while (cursor.moveToNext()) {
			String course_name = cursor.getString(cursor
					.getColumnIndex("Course_name"));
			String week = cursor.getString(cursor.getColumnIndex("Week"));
			String section_begin = cursor.getString(cursor
					.getColumnIndex("Section_begin"));
			
			String week_begin = cursor.getString(cursor
					.getColumnIndex("Week_begin"));
			String teacher_name = cursor.getString(cursor
					.getColumnIndex("Teacher_name"));		
			String course_alarm = cursor.getString(cursor
					.getColumnIndex("Course_alarm_time"));
			String course_exam = cursor.getString(cursor
					.getColumnIndex("Course_exam_time"));
			String course_other = cursor.getString(cursor
					.getColumnIndex("Course_other"));
			String room_name = cursor.getString(cursor
					.getColumnIndex("Room_name"));
			String section_end = cursor.getString(cursor
					.getColumnIndex("Section_end"));		
			String week_end = cursor.getString(cursor
					.getColumnIndex("Week_end"));		
			String course_begin_time = cursor.getString(cursor
					.getColumnIndex("Course_begin_time"));
			String course_end_time = cursor.getString(cursor
					.getColumnIndex("Course_end_time"));
			classList.add(course_name);//课程名称0
			classList.add(week);//section 周1
			classList.add(section_begin);//section start2
			classList.add(week_begin);//section end3
			classList.add(teacher_name);//老师4
			classList.add(course_alarm);//5
			classList.add(course_exam);//6
			classList.add(course_other);//7
			classList.add(room_name);//教室名称8
			classList.add(section_end);//9
			classList.add(week_end);//10
			classList.add(course_begin_time);//11
			classList.add(course_end_time);//12
			
		}
		cursor.close();
		db.close();
		return classList;
	}

	/* 选择课程表，有条件筛选，根据周数 */
	public ArrayList<String> selectCourseWeek(String weektime) {
		db = helper.getReadableDatabase();
		classList = new ArrayList<String>();
		Cursor cursor = db.rawQuery("select * from Course where week=?",
				new String[] { weektime });
		System.out.println("xxxx--->>" + cursor.getCount());
		while (cursor.moveToNext()) {
			String course_name = cursor.getString(cursor
					.getColumnIndex("Course_name"));
			String room_name = cursor.getString(cursor
					.getColumnIndex("Room_name"));
			String week = cursor.getString(cursor.getColumnIndex("Week"));
			String section_begin = cursor.getString(cursor
					.getColumnIndex("Section_begin"));
			String section_end = cursor.getString(cursor
					.getColumnIndex("Section_end"));
			classList.add(course_name);
			classList.add(room_name);
			classList.add(week);
			classList.add(section_begin);
			classList.add(section_end);
		}
		cursor.close();
		db.close();
		return classList;
	}

	public void deletCourse(String coursename) {
		db = helper.getReadableDatabase();
		String whereClause = "Course_name=?";// 删除的条件
		String[] whereArgs = { coursename };// 删除的条件参数
		db.delete("Course", whereClause, whereArgs);// 执行删除

	}

}
